package com.inossem.warehousestorage_inossem.activity.query;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.query.extra.QueryExtra;
import com.inossem.warehousestorage_inossem.bean.query.QueryWarehouseBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.query.QueryWarehouseRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

public class QueryBatchInventorySelectActivity extends BaseActivity {

    public static final int MATERIEL_CODE = 0;

    public static final int MATERIEL_DESCRIPTION = 1;

    public static final int POSITION_NUMBER = 2;

    private TextView title, nextStep;

    private TextView warehouseNumber, warehouseDescription, areaNumber, materielCode, materielDescription, positionNumber;

    private EditText condition;

    private PercentRelativeLayout warehouseHide;

    private List<QueryWarehouseBean.DataBean.WarehouseListBean> mWarehouseList;

    private int conditionType;

    @Override

    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_batch_inventory_select);
        initView(QueryBatchInventorySelectActivity.this);
        initData(QueryBatchInventorySelectActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        warehouseNumber = (TextView) activity.findViewById(R.id.warehouseNumber);
        warehouseDescription = (TextView) activity.findViewById(R.id.warehouseDescription);
        areaNumber = (TextView) activity.findViewById(R.id.areaNumber);
        materielCode = (TextView) activity.findViewById(R.id.materielCode);
        materielDescription = (TextView) activity.findViewById(R.id.materielDescription);
        positionNumber = (TextView) activity.findViewById(R.id.positionNumber);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        condition = (EditText) activity.findViewById(R.id.condition);
        warehouseHide = (PercentRelativeLayout) activity.findViewById(R.id.warehouseHide);
    }

    private void initData(Activity activity) {
        title.setText(getResources().getString(R.string.activity_query_batch_inventory_select_bulk_inventory_enquiries));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_free_list_inquiry), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });
        getData(activity);
    }

    private void setCondition(Context context, int index) {
        resetCondition(context);
        chooseCondition(context, index);
    }

    private void resetCondition(Context context) {
        materielCode.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
        materielDescription.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
        positionNumber.setTextColor(ContextCompat.getColor(context, R.color.activity_receive_delivery_overview_font_color));
        condition.setVisibility(View.GONE);
    }

    private void chooseCondition(Context context, int index) {
        switch (index) {
            case MATERIEL_CODE:
                materielCode.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                condition.setText("");
                condition.setHint(getResources().getString(R.string.activity_inventory_select_materiel_adapter_please_input_material_code));
                conditionType = MATERIEL_CODE;
                break;
            case MATERIEL_DESCRIPTION:
                materielDescription.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                condition.setText("");
                condition.setHint(getResources().getString(R.string.activity_query_batch_inventory_select_please_input_material_description));
                conditionType = MATERIEL_DESCRIPTION;
                break;
            case POSITION_NUMBER:
                positionNumber.setTextColor(ContextCompat.getColor(context, R.color.common_font_content));
                condition.setText("");
                condition.setHint(getResources().getString(R.string.activity_query_batch_inventory_select_please_enter_the_position_code));
                conditionType = POSITION_NUMBER;
                break;
        }
        condition.setVisibility(View.VISIBLE);
    }

    private void getData(Context context) {
        warehouseHide.setVisibility(View.GONE);
        QueryWarehouseRequest.request(context, new GsonBeanListener<QueryWarehouseBean>() {
            @Override
            public void onSuccess(QueryWarehouseBean bean) {
                mWarehouseList = bean.getData().getWarehouseList();
                initSpinner(context);
            }
        });

        materielCode.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                setCondition(context, MATERIEL_CODE);
            }
        });

        materielDescription.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                setCondition(context, MATERIEL_DESCRIPTION);
            }
        });

        positionNumber.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                setCondition(context, POSITION_NUMBER);
            }
        });

        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String checkResult = check(context);
                if (checkResult == null) {
                    Intent intent = new Intent(context, QueryBatchMaterielListActivity.class);
                    intent.putExtra(QueryExtra.CONDITION, condition.getText().toString());
                    intent.putExtra(QueryExtra.CONDITION_TYPE, conditionType);
                    intent.putExtra(QueryExtra.WAREHOUSE_NUMBER, warehouseNumber.getText().toString());
                    intent.putExtra(QueryExtra.AREA_NUMBER, areaNumber.getText().toString());
                    startActivity(intent);
                } else {
                    Utils.middleToast(context, checkResult);
                }
            }
        });

        setCondition(context, MATERIEL_DESCRIPTION);
        setmScanListener((String scanResult, int length) -> {
            String type = QRCodeUtils.getQRCodeType(scanResult);
            switch (conditionType) {
                case MATERIEL_CODE:
                    if (QRCodeConstant.TYPE_MATERIEL_CODE.equals(type)) {
                        condition.setText(QRCodeUtils.DecryptQRCode(scanResult));
                    } else if (QRCodeConstant.TYPE_BATCH.equals(type)) {
                        condition.setText(QRCodeUtils.DecryptQRCode(scanResult, QRCodeConstant.PARAMETER_MATERIEL_CODE));
                    } else {
                        Utils.middleToast(context, getResources().getString(R.string.activity_query_batch_inventory_select_material_encoding_informatione));
                    }
                    break;
                case MATERIEL_DESCRIPTION:
                    Utils.middleToast(context, getResources().getString(R.string.activity_query_batch_inventory_select_material_description_does_not_support_scan_code));
                    break;
                case POSITION_NUMBER:
                    if (QRCodeConstant.TYPE_POSITION.equals(type)) {
                        String position = QRCodeUtils.DecryptQRCode(scanResult);
                        if (position.contains("-")) {
                            String areaNumberResult = position.substring(0, position.indexOf("-"));
                            if (areaNumber.getText().toString().equals(areaNumberResult)) {
                                String positionResult = position.substring(position.indexOf("-") + 1, position.length());
                                condition.setText(positionResult);
                            } else {
                                Utils.middleToast(context, getResources().getString(R.string.activity_query_batch_inventory_select_the_storage_area_in_the_position_code_is_not_in_conformitye));
                            }
                        } else {
                            Utils.middleToast(context, getResources().getString(R.string.activity_query_batch_inventory_select_problem_with_the_position_code));
                        }
                    } else {
                        Utils.middleToast(context, getResources().getString(R.string.activity_query_batch_inventory_select_position_information_only_supports_scanning_the_position_codee));
                    }
                    break;
            }
        }, QRCodeConstant.TYPE_BATCH, QRCodeConstant.TYPE_MATERIEL_CODE, QRCodeConstant.TYPE_POSITION);
    }

    private String check(Context context) {
        if (context.getResources().getString(R.string.activity_query_manage_warehouse_hint).equals(warehouseNumber.getText().toString())) {
            return context.getResources().getString(R.string.activity_query_manage_warehouse_hint);
        }
        if (context.getResources().getString(R.string.activity_query_manage_area_hint).equals(areaNumber.getText().toString())) {
            return context.getResources().getString(R.string.activity_query_manage_area_hint);
        }
        return null;
    }

    private void resetViewWithoutFactory(Context context) {
        areaNumber.setText(context.getResources().getString(R.string.activity_query_manage_area_hint));
        warehouseHide.setVisibility(View.GONE);
    }

    private void initSpinner(Context context) {
        warehouseNumber.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getWarehouseDialog(context, mWarehouseList, new DialogUtils.WarehouseListener() {
                    @Override
                    public void onSelect(Dialog dialog, String warehouseNumberStr, String warehouseDescriptionStr, List<QueryWarehouseBean.DataBean.WarehouseListBean.AreaListBean> areaList) {
                        if (!warehouseNumberStr.equals(warehouseNumber.getText().toString())) {
                            warehouseNumber.setText(warehouseNumberStr);
                            warehouseDescription.setText(warehouseDescriptionStr);
                            resetViewWithoutFactory(context);
                            warehouseHide.setVisibility(View.VISIBLE);
                            areaNumber.setOnClickListener(new NoDoubleClickListener() {
                                @Override
                                public void onNoDoubleClick(View v) {
                                    DialogUtils.getAreaDialog(context, areaList, new DialogUtils.AreaListener() {
                                        @Override
                                        public void onSelect(Dialog dialog, String areaNumberStr, String areaDescriptionStr) {
                                            areaNumber.setText(areaNumberStr);
                                        }
                                    }).show();
                                }
                            });
                        }
                    }
                }).show();
            }
        });
    }

//    @Override
//    public void onBackPressed() {
//        DialogUtils.getConfirmDialog(BatchInventorySelectActivity.this, "退出后将清空所填信息", new DialogUtils.ConfirmDialogListener() {
//            @Override
//            public void onConfirm(Dialog dialog) {
//                BatchInventorySelectActivity.super.onBackPressed();
//            }
//        }).show();
//    }
}
